package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybb.vue.common.R;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.OperationRecordsUtils;
import com.ybb.vue.common.webSocket.WebSocket;
import com.ybb.vue.domain.dto.AnnouncementSaveDto;
import com.ybb.vue.domain.vo.AnnIndexVo;
import com.ybb.vue.entity.Announcement;
import com.ybb.vue.entity.EpmAlarmRule;
import com.ybb.vue.mapper.AnnouncementMapper;
import com.ybb.vue.service.AnnouncementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description 公告管理controller
 * @Date 2023/3/30 21:17
 */

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private WebSocket webSocket;

    /**
     * 查询announcement列表
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/announcementList")
    @PreAuthorize("hasAuthority('announcement:management:list')")
    public Result<?> announcementList (@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Announcement> queryWrapper = Wrappers.<Announcement>lambdaQuery();
        queryWrapper.like(Announcement::getPublisher, search);
        queryWrapper.orderByDesc(Announcement::getCreateTime);
        Page<Announcement> result = announcementService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(result);
    }

    /**
     * 查询announcement列表  首页使用
     * @return
     */
    @GetMapping("/announcementListIndex")
    public Result<?> announcementListIndex () {
        List<AnnIndexVo> list = announcementMapper.selectAllAnnouncements();
        return Result.success(list);
    }

    /**
     * 添加
     * @param announcementSaveDto
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('announcement:management:add')")
    public Result<?> save(@RequestBody @Valid AnnouncementSaveDto announcementSaveDto){
        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementSaveDto, announcement);
        announcement.setPublisher(SecurityContextHolder.getContext().getAuthentication().getName());
        announcementService.save(announcement);

        // 操作记录
        String content = "操作了公告表，【新增】了1条记录，记录id为：" + announcement.getId();
        OperationRecordsUtils.createOperationRecords(content);

        // 发送webscoket消息
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AnnIndexVo annIndexVo = new AnnIndexVo();
        BeanUtils.copyProperties(announcement, annIndexVo);
        annIndexVo.setReturnMessageType("A");
        webSocket.sendMessage(annIndexVo);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('announcement:management:delete')")
    public R delete(@RequestBody Long[] ids){
        announcementService.removeByIds(Arrays.asList(ids));

        // 操作记录
        String content = "操作了公告表，【删除】了"+Arrays.asList(ids).size()+"条记录，记录id为：" + ids;
        OperationRecordsUtils.createOperationRecords(content);
        return R.ok();
    }
}
